package org.xiaobai.tool.utils;

import org.jasypt.encryption.pbe.StandardPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.EnvironmentPBEConfig;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;
import java.util.Random;

public class JasyptUtil {

    public static void testEncrypt() {

        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setPassword("123456");
        standardPBEStringEncryptor.setConfig(config);
        String plainText = "123456";
        String encryptText = standardPBEStringEncryptor.encrypt(plainText);
        System.out.println(encryptText);
    }

    public static void testDecrypt() {

        StandardPBEStringEncryptor standardPBEStringEncryptor = new StandardPBEStringEncryptor();
        EnvironmentPBEConfig config = new EnvironmentPBEConfig();
        config.setAlgorithm("PBEWithMD5AndDES");
        config.setPassword("0000");
        standardPBEStringEncryptor.setConfig(config);
        String encryptText = "r5c/qpcCtAH7ADfHmyI/7GAQ4251dQbjLLal/7IedII=";
        String plainText = standardPBEStringEncryptor.decrypt(encryptText);
        System.out.println(plainText);
    }

    public static void main(String[] args) {

        System.out.println("========加密后的密文========");
        testEncrypt();
        System.out.println("========解密后的明文========");
        testDecrypt();
    }

    public static String finalShellDecode(String password) throws Exception {
        return decode(password);
    }

    private static String decode(String str) throws Exception {
        if (str == null) {
            return null;
        } else {
            byte[] buf = Base64.getDecoder().decode(str);
            byte[] head = new byte[8];
            System.arraycopy(buf, 0, head, 0, head.length);
            byte[] d = new byte[buf.length - head.length];
            System.arraycopy(buf, head.length, d, 0, d.length);

            SecureRandom sr = new SecureRandom();
            DESKeySpec dks = new DESKeySpec(ranDomKey(head));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey securekey = keyFactory.generateSecret(dks);
            Cipher cipher = Cipher.getInstance("DES");
            cipher.init(2, securekey, sr);
            byte[] bt = cipher.doFinal(d);
            return new String(bt);
        }
    }


    private static byte[] ranDomKey(byte[] head) {
        long ks = 3680984568597093857L / (long) (new Random((long) head[5])).nextInt(127);
        Random random = new Random(ks);
        int t = head[0];

        for (int i = 0; i < t; ++i) {
            random.nextLong();
        }

        long n = random.nextLong();
        Random r2 = new Random(n);
        long[] ld = new long[]{(long) head[4], r2.nextLong(), (long) head[7], (long) head[3], r2.nextLong(), (long) head[1], random.nextLong(), (long) head[2]};
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(bos);
        for (long l : ld) {
            try {
                dos.writeLong(l);
            } catch (IOException var18) {
                var18.printStackTrace();
            }
        }

        try {
            dos.close();
        } catch (IOException var17) {
            var17.printStackTrace();
        }

        byte[] keyData = bos.toByteArray();
        keyData = md5(keyData);
        return keyData;
    }

    private static byte[] md5(byte[] data) {
        byte[] res = null;
        try {
            MessageDigest m;
            m = MessageDigest.getInstance("MD5");
            m.update(data, 0, data.length);
            res = m.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return res;
    }
}

